package com.qbw.app.module.vehicle.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import com.http.okhttp.HttpTask;
import com.qbw.app.Constant;
import com.qbw.app.R;
import com.qbw.app.databinding.VehicleActivityDetailBinding;
import com.qbw.app.model.Vehicle;
import com.qbw.app.module.common.activity.CommonActivity;
import com.qbw.app.module.common.fragment.InputDialogFragment;
import com.qbw.app.module.common.fragment.OkCancelDialogFragment;
import com.qbw.app.module.common.util.HttpRequestUtil;
import com.qbw.app.module.common.util.UniversalToastUtil;
import com.qbw.app.request.impl.VehicleRequest;

/**
 * @author QBW
 * @date 2019/9/26
 */
public class VehicleDetailActivity extends CommonActivity<VehicleActivityDetailBinding> implements OkCancelDialogFragment.Listener, InputDialogFragment.Listener {

    private String mVehicleCode;

    public static void start(Context context, String vehicleCode) {
        Intent intent = new Intent(context, VehicleDetailActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        intent.putExtra(Constant.KEY_VEHICLE_CODE, vehicleCode);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.vehicle_activity_detail;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = savedInstanceState == null ? getIntent().getExtras() : savedInstanceState;
        mVehicleCode = args.getString(Constant.KEY_VEHICLE_CODE);
        mBinding.title.setListener(this);
        mBinding.clNickname.setOnClickListener(this);
        mBinding.tvDelete.setOnClickListener(this);
        showLoadingDialog("");
        mGeneralManager.getRequestManager()
                .getVehicleRequest()
                .getVehicleDetail(mVehicleCode, this)
                .execute();

    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    public void onClick(View v) {
        int vid = v.getId();
        if (vid == R.id.tv_delete) {
            OkCancelDialogFragment.createInstance(0, R.string.delete_vehicle_tip, Gravity.LEFT)
                                  .show(getSupportFragmentManager(), "delete");
        } else if (vid == R.id.cl_nickname) {
            if (mBinding.getVehicle() == null) {
                return;
            }
            InputDialogFragment.createInstance(R.id.tv_nickname,
                                               gs(R.string.vehicle_name),
                                               gs(R.string.vehicle_name_hint),
                                               "",
                                               mBinding.getVehicle().getVehicleNick(),
                                               30).show(getSupportFragmentManager(), "nickname");
        }
//        else if (vid == R.id.tv_number) {
//            if (mBinding.getVehicle() == null) {
//                return;
//            }
//            InputDialogFragment.createInstance(R.id.tv_number,
//                                               gs(R.string.vehicle_number),
//                                               gs(R.string.vehicle_number_hint),
//                                               "",
//                                               mBinding.getVehicle().getVehicleNumber(),
//                                               0).show(getSupportFragmentManager(), "number");
//        }
    }

    @Override
    public void onHttpSuccess(HttpTask httpTask, Object entity) {
        super.onHttpSuccess(httpTask, entity);
        String method = httpTask.getMethod();
        if (method.equals(VehicleRequest.METHOD_VEHICLE_DETAIL)) {
            Vehicle.ResponseOne responseOne = (Vehicle.ResponseOne) entity;
            mBinding.setVehicle(responseOne.getData());
            closeLoadingDialog();
        } else if (method.equals(VehicleRequest.METHOD_DELETE_VEHICLE)) {
            closeLoadingDialog();
            finish();
        } else if (method.equals(VehicleRequest.METHOD_EDIT_VEHICLE)) {
            int type = (int) httpTask.getExtraParam(Constant.KEY_TYPE);
            String value = (String) httpTask.getExtraParam(Constant.KEY_VALUE);
            if (type == R.id.tv_nickname) {
                mBinding.getVehicle().setVehicleNick(value);
            }
            closeLoadingDialog();
        }
    }

    @Override
    public void onHttpFailed(HttpTask httpTask, int errorCode, String message) {
        super.onHttpFailed(httpTask, errorCode, message);
        message = HttpRequestUtil.getMessage(errorCode, message);
        String method = httpTask.getMethod();
        if (method.equals(VehicleRequest.METHOD_VEHICLE_DETAIL)) {
            closeLoadingDialog();
        } else if (method.equals(VehicleRequest.METHOD_DELETE_VEHICLE)) {
            UniversalToastUtil.showFailedIconToast(message);
            closeLoadingDialog();
        } else if (method.equals(VehicleRequest.METHOD_EDIT_VEHICLE)) {
            UniversalToastUtil.showFailedIconToast(message);
            closeLoadingDialog();
        }
    }

    @Override
    public void onClickSure(int type) {
        showLoadingDialog("");
        mGeneralManager.getRequestManager()
                       .getVehicleRequest()
                       .deleteVehicle(mVehicleCode, this)
                       .execute();
    }

    @Override
    public void onClickCancel(int type) {

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(Constant.KEY_VEHICLE_CODE, mVehicleCode);
    }

    @Override
    public void onClickInputOk(int type, String inputContent) {
        if (TextUtils.isEmpty(inputContent)) {
            return;
        }
        if (type == R.id.tv_nickname) {
            showLoadingDialog("");
            mGeneralManager.getRequestManager()
                           .getVehicleRequest()
                           .editVehicle(mVehicleCode, null, inputContent, this)
                           .addExtraParam(Constant.KEY_TYPE, type)
                           .addExtraParam(Constant.KEY_VALUE, inputContent)
                           .execute();
        }
//        else if (type == R.id.tv_number) {
//            showLoadingDialog("");
//            mGeneralManager.getRequestManager()
//                           .getVehicleRequest()
//                           .editVehicle(mVehicleCode, inputContent, null, this)
//                           .addExtraParam(Constant.KEY_TYPE, type)
//                           .addExtraParam(Constant.KEY_VALUE, inputContent)
//                           .execute();
//        }
    }

    @Override
    public void onClickInputCancel(int type) {

    }

}
